package com.ayoka.mvp;

import android.support.v4.app.FragmentManager;

import com.ayoka.Constant;
import com.ayoka.R;
import com.ayoka.base.BasePresenter;
import com.ayoka.base.IBaseView;
import com.ayoka.bean.BaseResponse;
import com.ayoka.bean.TrainingBean;
import com.ayoka.http.API;
import com.ayoka.http.OkHttpListener;
import com.ayoka.mvp.TrainingGroupMVP.TrainingGroupModel.TrainingGroupBean;
import com.ayoka.mvp.TrainingGroupMVP.TrainingGroupModel.TrainingGroupSectionEntity;
import com.ayoka.widget.TwoButtonDialog;
import com.jcp.baselibrary.brvah.entity.SectionEntity;
import com.jcp.baselibrary.dialog.BaseDialogFragment;
import com.jcp.baselibrary.dialog.DialogResultListener;
import com.jcp.baselibrary.utils.UIUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by gabriel on 2017/3/5.
 */

public interface TrainingGroupMVP {

    interface TrainingGroupModel {

        class TrainingGroupSectionEntity extends SectionEntity<TrainingBean> {

            public String subTitle;

            public TrainingGroupSectionEntity(boolean isHeader, String header, String subTitle) {
                super(isHeader, header);
                this.subTitle = subTitle;
            }

            public TrainingGroupSectionEntity(TrainingBean trainingBean) {
                super(trainingBean);
            }
        }

        class TrainingGroupBean {
            public List<TrainingBean> basic_list;
            public List<TrainingBean> advanced_list;
        }
    }

    interface ITrainingGroupView extends IBaseView {

        void configTrainingGroupData(TrainingGroupBean trainingGroupBean);

        void onOpenMemberClick();
    }

    class TrainingGroupPresenter extends BasePresenter<ITrainingGroupView> {

        public void getTrainingGroupData(boolean isShowWaitDialog, long courseId, long groupId) {

            if (isShowWaitDialog) {
                mViewRef.get().showWaitDialog(UIUtils.getString(R.string.loading));
            }
            if (courseId != 0) {
                API.getMeditationListDataByCourseId(this, courseId, okHttpListener);
            } else if (groupId != 0) {
                API.getMeditationListDataByPackageId(this, groupId, okHttpListener);
            } else {
                mViewRef.get().hideWaitDialog();
            }
        }

        private OkHttpListener okHttpListener = new OkHttpListener<BaseResponse<TrainingGroupBean>>() {
            @Override
            public void onSuccess(BaseResponse<TrainingGroupBean> response) {
                if (!isAttached()) return;
                mViewRef.get().hideWaitDialog();
                mViewRef.get().configTrainingGroupData(response.data);
            }

            @Override
            public void onFailure(int errorCode, String errorMessage) {
                if (!isAttached()) return;
                mViewRef.get().hideWaitDialog();
                mViewRef.get().showToast(errorMessage);
            }
        };

        public List<TrainingGroupSectionEntity> formatList(TrainingGroupBean trainingGroupBean) {
            List<TrainingGroupSectionEntity> result = new ArrayList<>();
            if (trainingGroupBean == null) return result;
            if (trainingGroupBean.basic_list != null && !trainingGroupBean.basic_list.isEmpty()) {
//                result.add(new TrainingGroupSectionEntity(true, "基础练习", "（完成后解锁下一课）"));
                for (TrainingBean trainingBean : trainingGroupBean.basic_list) {
                    result.add(new TrainingGroupSectionEntity(trainingBean.setTrainingType(Constant.TrainingType.BASIC)));
                }
            }
            if (trainingGroupBean.advanced_list != null && !trainingGroupBean.advanced_list.isEmpty()) {
//                result.add(new TrainingGroupSectionEntity(true, "进阶练习", "（会员专享）"));
                for (TrainingBean trainingBean : trainingGroupBean.advanced_list) {
                    result.add(new TrainingGroupSectionEntity(trainingBean.setTrainingType(Constant.TrainingType.ADVANCED)));
                }
            }
            return result;
        }

        public List<TrainingGroupSectionEntity> formatAdvanceList(TrainingGroupBean trainingGroupBean) {
            List<TrainingGroupSectionEntity> result = new ArrayList<>();
            if (trainingGroupBean == null) return result;
            if (trainingGroupBean.advanced_list != null && !trainingGroupBean.advanced_list.isEmpty()) {
                result.add(new TrainingGroupSectionEntity(true, "进阶练习", ""));
                for (TrainingBean trainingBean : trainingGroupBean.advanced_list) {
                    result.add(new TrainingGroupSectionEntity(trainingBean.setTrainingType(Constant.TrainingType.ADVANCED)));
                }
            }
            return result;
        }

        public void showOpenMemberDialog(FragmentManager fragmentManager, String message) {
            TwoButtonDialog dialog = new TwoButtonDialog.Builder()
                    .setTitle(UIUtils.getString(R.string.point_out))
                    .setMessage(message)
                    .setNegativeText(UIUtils.getString(R.string.cancel))
                    .setPositiveText(UIUtils.getString(R.string.open_member))
                    .build();
            dialog.setDialogResultListener(new DialogResultListener<Boolean>() {
                @Override
                public void result(BaseDialogFragment DialogFragment, Boolean result) {
                    if (!isAttached()) return;
                    if (result) {
                        mViewRef.get().onOpenMemberClick();
                    }
                }
            });
            dialog.show(fragmentManager, "TwoButtonDialog");
        }
    }

}
